package com.neborosoft.android.gui;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.neborosoft.lib.handlers.OnHardwareBackPressed;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 04.01.13
 * Time: 23:54
 * To change this template use File | Settings | File Templates.
 */
public class AutoKeyboardHandlingEditTextView extends EditText{
    private OnHardwareBackPressed onHardwareBackPressedListener;
    private OnClickListener onEnterClickListener;

    public OnHardwareBackPressed getOnHardwareBackPressedListener() {
        return onHardwareBackPressedListener;
    }

    public void setOnHardwareBackPressedListener(OnHardwareBackPressed onHardwareBackPressedListener) {
        this.onHardwareBackPressedListener = onHardwareBackPressedListener;
    }

    public OnClickListener getOnEnterClickListener() {
        return onEnterClickListener;
    }

    public void setOnEnterClickListener(OnClickListener onEnterClickListener) {
        this.onEnterClickListener = onEnterClickListener;
    }

    public AutoKeyboardHandlingEditTextView(Context context) {
        super(context);
    }

    public AutoKeyboardHandlingEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoKeyboardHandlingEditTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void showKeyboard(){
        InputMethodManager inputMethodManager =
                (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
    }

    public void hideKeyboard(){
        InputMethodManager inputMethodManager =
                (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
    }

    public void startEditing(){
        requestFocus();
        showKeyboard();
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_UP){
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if(onHardwareBackPressedListener != null){
                        onHardwareBackPressedListener.onBack();
                    }
                    break;
            }
        }
        return super.onKeyPreIme(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch(keyCode) {
            case KeyEvent.KEYCODE_SEARCH:
            case KeyEvent.KEYCODE_ENTER:
                if(onEnterClickListener != null){
                    onEnterClickListener.onClick(this);
                }
                hideKeyboard();
                clearFocus();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        return super.requestFocus(direction, previouslyFocusedRect);
    }
}
